<?php
/**
 * 这里是登录操作
 * 
 * @author jerry
 * @version $Id$
 * @package model
 *
 */

class info_model extends Model
{
	/**
	 * 解析函数
	 *
	 */
	function __construct()
	{
		parent::__construct();
		
		$this->setOrder("order by Top Desc , AreaID ASC");
		$this->area_list = $this->getDataList('area');
		$this->cleanConditions();
		$this->setOrder("order by Top Desc , CategoryID ASC");
		$this->category_list = $this->getDataList('category');
	}

	function getList($select = '*',$order = ' order by Id desc',$page = 0,$num = 0)
	{
		$this->setLimit($num);
		$this->setPage($page);
		$this->setOrder($order);

		return $this->getDataList('info',$select);
	}

	//获取分类
	function getCategoryForSelect($root_id = 0,$currently_id = 0,$pre = '&nbsp;&#8250;&nbsp;',$level = 0)
	{
		if(!empty($this->category_list))
		{
			foreach ($this->category_list as $key => $value)
			{
				if($root_id == $value['ParentID'])
				{
					for($i=0;$i<=$level;$i++)
					{
						$currently_pre .= $pre;
					}

					if($currently_id == $value['CategoryID'])
					{
						$this->category_select_list .= '<option value="' . $value['CategoryID'] . '" selected="selected">' . $currently_pre . $value['CategoryName'] . '</option>' . "\n";
					}
					else
					{
						$this->category_select_list .= '<option value="' . $value['CategoryID'] . '">' . $currently_pre . $value['CategoryName'] . '</option>' . "\n";
					}

					unset($currently_pre);
					$this->getCategoryForSelect($value['CategoryID'],$currently_id,$pre,$level + 1);
				}
			}
		}

		return $this->category_select_list;
	}


	//获取地区
	//获取分类
	function getAreaForSelect($root_id = 0,$currently_id = 0,$pre = '&nbsp;&#8250;&nbsp;',$level = 0)
	{

		if(!empty($this->area_list))
		{
			foreach ($this->area_list as $key => $value)
			{
				if($root_id == $value['ParentID'])
				{
					for($i=0;$i<=$level;$i++)
					{
						$currently_pre .= $pre;
					}

					if($currently_id == $value['AreaID'])
					{
						$this->area_select_list .= '<option value="' . $value['AreaID'] . '" selected="selected">' . $currently_pre . $value['AreaName'] . '</option>' . "\n";
					}
					else
					{
						$this->area_select_list .= '<option value="' . $value['AreaID'] . '">' . $currently_pre . $value['AreaName'] . '</option>' . "\n";
					}

					unset($currently_pre);
					$this->getAreaForSelect($value['AreaID'],$currently_id,$pre,$level + 1);
				}
			}
		}

		return $this->area_select_list;
	}


	function getInfo($id)
	{
		$this->where('Id',$id);
		return $this->getDataInfo('info');
	}


	function add()
	{
		if($this->in_var['request_method'] == 'post')
		{

			$this->addData('Title',$this->in_var['Title']);
			$this->addData('Phone',$this->in_var['Phone']);
			$this->addData('Intro',$this->in_var['Intro']);
			$this->addData('PublishDate',time());
			$this->addData('State',$this->in_var['State']);
			$this->addData('Top',$this->in_var['Top']);

			$this->addData('CategoryID',$this->in_var['CategoryID']);
			$this->addData('AreaID',$this->in_var['AreaID']);
			$this->addData('BgStyle',$this->in_var['BgStyle']);

			return $this->dataInsert('info');
		}

	}


	function edit()
	{
		$this->addData('Title',$this->in_var['Title']);
		$this->addData('Phone',$this->in_var['Phone']);
		$this->addData('Intro',$this->in_var['Intro']);
		$this->addData('State',$this->in_var['State']);
		$this->addData('Top',$this->in_var['Top']);

		$this->addData('CategoryID',$this->in_var['CategoryID']);
		$this->addData('AreaID',$this->in_var['AreaID']);
		$this->addData('BgStyle',$this->in_var['BgStyle']);
		
		$this->where('Id',$this->in_var['id']);
		return $this->dataUpdate('info');
	}

	function pass($state)
	{
		$this->addData('State',$state);
		$this->where('Id', $this->in_var['id']);
		return $this->dataUpdate('info');
	}

	function delete($id)
	{
		$this->where('Id',$id);
		return $this->dataDel('info');
	}


}